package com.liveinstars.im.domain.manager.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.api.im.query.TodoQuery;
import com.liveinstars.im.domain.dao.mapper.TodoMapper;
import com.liveinstars.im.domain.dao.model.Todo;
import com.liveinstars.im.domain.manager.TodoManager;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代办事项表 服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-03-31
 */
@Service
public class TodoManagerImpl extends ServiceImpl<TodoMapper, Todo> implements TodoManager {

    private final static Logger logger = LoggerFactory.getLogger(TodoManagerImpl.class);

    @Override
    public List<Todo> listByQuery(TodoQuery query) {
        try {
            QueryWrapper<Todo> queryWrapper = new QueryWrapper<>();
            if (query.getComplete() != null) {
                queryWrapper.eq("complete", query.getComplete());
            }
            if (query.getChatGroupId() != null) {
                queryWrapper.eq("group_id", query.getChatGroupId());
            }
            if (query.getActivityId() != null) {
                queryWrapper.eq("activity_id", query.getActivityId());
            }
            if (query.getFromId() != null) {
                queryWrapper.eq("from_id", query.getFromId());
            }
            if (query.getToId() != null) {
                queryWrapper.eq("user_id", query.getToId());
            }
            if (query.getStartId() != null) {
                queryWrapper.gt("id", query.getStartId());
            }
            queryWrapper.orderByDesc("id");
            List<Todo> list = this.list(queryWrapper);
            return list;
        } catch (Exception e) {
            logger.error("listByQuery error：params: query={} errorMsg:{}", JSON.toJSONString(query), e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public Todo getById(Long id) {
        if (id == null) {
            throw new DatabaseException(SystemExceptionEnum.MISSING_REQUEST_PARAM);
        }
        try {
            Todo todo = super.getById(id);
            return todo;
        } catch (Exception e) {
            logger.error("getById error：params: id={} errorMsg:{}", id, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public List<Todo> listUnCompleteActivityTodoByUserId(Long userId) {
        try {
            QueryWrapper<Todo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("complete", false);
            queryWrapper.eq("from_id", userId);
            queryWrapper.gt("activity_id", 0);
            queryWrapper.orderByDesc("id");
            List<Todo> list = this.list(queryWrapper);
            return list;
        } catch (Exception e) {
            logger.error("listByQuery error：params: query={} errorMsg:{}", userId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
